Skip to content

Commit 74beb3e

Browse files
committed
[MLIR] Enable import of non self referential alias scopes
1 parent 6192faf commit 74beb3e

File tree

4 files changed

+91
-12
lines changed

4 files changed

+91
-12
lines changed

mlir/include/mlir/Dialect/LLVMIR/LLVMAttrDefs.td

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -825,7 +825,7 @@ def LLVM_MemoryEffectsAttr : LLVM_Attr<"MemoryEffects", "memory_effects"> {
825825
def LLVM_AliasScopeDomainAttr : LLVM_Attr<"AliasScopeDomain",
826826
"alias_scope_domain"> {
827827
let parameters = (ins
828-
"DistinctAttr":$id,
828+
"Attribute":$id,
829829
OptionalParameter<"StringAttr">:$description
830830
);
831831

@@ -853,7 +853,7 @@ def LLVM_AliasScopeDomainAttr : LLVM_Attr<"AliasScopeDomain",
853853

854854
def LLVM_AliasScopeAttr : LLVM_Attr<"AliasScope", "alias_scope"> {
855855
let parameters = (ins
856-
"DistinctAttr":$id,
856+
"Attribute":$id,
857857
"AliasScopeDomainAttr":$domain,
858858
OptionalParameter<"StringAttr">:$description
859859
);
@@ -891,6 +891,8 @@ def LLVM_AliasScopeAttr : LLVM_Attr<"AliasScope", "alias_scope"> {
891891
}
892892
```
893893

894+
The first attribute can either be a DistinctAttribute or a StringAttribute.
895+
894896
See the following link for more details:
895897
https://llvm.org/docs/LangRef.html#noalias-and-alias-scope-metadata
896898
}];

mlir/lib/Target/LLVMIR/ModuleImport.cpp

Lines changed: 41 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -438,8 +438,14 @@ ModuleImport::processAliasScopeMetadata(const llvm::MDNode *node) {
438438
if (aliasDomain->getNumOperands() >= 2)
439439
if (auto *operand = dyn_cast<llvm::MDString>(aliasDomain->getOperand(1)))
440440
description = builder.getStringAttr(operand->getString());
441-
return builder.getAttr<AliasScopeDomainAttr>(
442-
DistinctAttr::create(builder.getUnitAttr()), description);
441+
if (verifySelfRef(aliasDomain))
442+
return builder.getAttr<AliasScopeDomainAttr>(
443+
DistinctAttr::create(builder.getUnitAttr()), description);
444+
else {
445+
auto Name = cast<llvm::MDString>(aliasDomain->getOperand(0));
446+
return builder.getAttr<AliasScopeDomainAttr>(
447+
builder.getStringAttr(Name->getString()), description);
448+
}
443449
};
444450

445451
// Collect the alias scopes and domains to translate them.
@@ -452,12 +458,28 @@ ModuleImport::processAliasScopeMetadata(const llvm::MDNode *node) {
452458
// verifying its domain. Perform the verification before looking it up in
453459
// the alias scope mapping since it could have been inserted as a domain
454460
// node before.
455-
if (!verifySelfRef(scope) || !domain || !verifyDescription(scope, 2))
456-
return emitError(loc) << "unsupported alias scope node: "
461+
if (!domain)
462+
return emitError(loc) << "unsupported alias scope node (no domain): "
457463
<< diagMD(scope, llvmModule.get());
458-
if (!verifySelfRef(domain) || !verifyDescription(domain, 1))
459-
return emitError(loc) << "unsupported alias domain node: "
460-
<< diagMD(domain, llvmModule.get());
464+
if (verifySelfRef(scope) && !verifyDescription(scope, 2))
465+
return emitError(loc)
466+
<< "unsupported alias scope node (bad description): "
467+
<< diagMD(scope, llvmModule.get());
468+
if (!verifySelfRef(scope) && (scope->getNumOperands() == 0 ||
469+
!isa<llvm::MDString>(scope->getOperand(0))))
470+
return emitError(loc)
471+
<< "unsupported alias scope node (not self-ref and no string): "
472+
<< diagMD(scope, llvmModule.get());
473+
if (!verifyDescription(domain, 1))
474+
return emitError(loc)
475+
<< "unsupported alias domain node (bad description): "
476+
<< diagMD(domain, llvmModule.get());
477+
if (!verifySelfRef(domain) &&
478+
(domain->getNumOperands() == 0 ||
479+
!isa<llvm::MDString>(domain->getOperand(0))))
480+
return emitError(loc)
481+
<< "unsupported alias domain node (not self ref and no string): "
482+
<< diagMD(scope, llvmModule.get());
461483

462484
if (aliasScopeMapping.contains(scope))
463485
continue;
@@ -473,9 +495,18 @@ ModuleImport::processAliasScopeMetadata(const llvm::MDNode *node) {
473495
StringAttr description = nullptr;
474496
if (!aliasScope.getName().empty())
475497
description = builder.getStringAttr(aliasScope.getName());
476-
auto aliasScopeOp = builder.getAttr<AliasScopeAttr>(
477-
DistinctAttr::create(builder.getUnitAttr()),
478-
cast<AliasScopeDomainAttr>(it->second), description);
498+
AliasScopeAttr aliasScopeOp;
499+
if (verifySelfRef(scope))
500+
aliasScopeOp = builder.getAttr<AliasScopeAttr>(
501+
DistinctAttr::create(builder.getUnitAttr()),
502+
cast<AliasScopeDomainAttr>(it->second), description);
503+
else {
504+
auto Name = cast<llvm::MDString>(scope->getOperand(0));
505+
aliasScopeOp = builder.getAttr<AliasScopeAttr>(
506+
builder.getStringAttr(Name->getString()),
507+
cast<AliasScopeDomainAttr>(it->second), description);
508+
}
509+
479510
aliasScopeMapping.try_emplace(aliasScope.getNode(), aliasScopeOp);
480511
}
481512
}

mlir/test/Dialect/LLVMIR/roundtrip.mlir

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -750,6 +750,16 @@ llvm.func @experimental_noalias_scope_decl() {
750750
llvm.return
751751
}
752752

753+
#alias_scope_domain2 = #llvm.alias_scope_domain<id = "domainid", description = "The domain">
754+
#alias_scope2 = #llvm.alias_scope<id = "stringid", domain = #alias_scope_domain2, description = "The domain">
755+
756+
// CHECK-LABEL: @experimental_noalias_scope_decl
757+
llvm.func @experimental_noalias_scope_decl2() {
758+
// CHECK: llvm.intr.experimental.noalias.scope.decl #{{.*}}
759+
llvm.intr.experimental.noalias.scope.decl #alias_scope2
760+
llvm.return
761+
}
762+
753763
// CHECK-LABEL: @experimental_constrained_fptrunc
754764
llvm.func @experimental_constrained_fptrunc(%in: f64) {
755765
// CHECK: llvm.intr.experimental.constrained.fptrunc %{{.*}} towardzero ignore : f64 to f32

mlir/test/Target/LLVMIR/Import/metadata-alias-scopes.ll

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,3 +92,39 @@ declare void @foo(ptr %arg1)
9292
!0 = distinct !{!0, !"The domain"}
9393
!1 = !{!1, !0}
9494
!2 = !{!1}
95+
96+
; // -----
97+
98+
; CHECK: #[[DOMAIN:.*]] = #llvm.alias_scope_domain<id = "domain1">
99+
; CHECK: #[[$SCOPE0:.*]] = #llvm.alias_scope<id = "scopeid1", domain = #[[DOMAIN]], description = "The first scope">
100+
; CHECK: #[[$SCOPE1:.*]] = #llvm.alias_scope<id = "scopeid2", domain = #[[DOMAIN]]>
101+
; CHECK: #[[$SCOPE2:.*]] = #llvm.alias_scope<id = "scopeid3", domain = #[[DOMAIN]]>
102+
103+
; CHECK-LABEL: llvm.func @alias_scope
104+
define void @alias_scope(ptr %arg1) {
105+
; CHECK: llvm.load
106+
; CHECK-SAME: alias_scopes = [#[[$SCOPE0]]]
107+
; CHECK-SAME: noalias_scopes = [#[[$SCOPE1]], #[[$SCOPE2]]]
108+
%1 = load i32, ptr %arg1, !alias.scope !4, !noalias !7
109+
; CHECK: llvm.load
110+
; CHECK-SAME: alias_scopes = [#[[$SCOPE1]]]
111+
; CHECK-SAME: noalias_scopes = [#[[$SCOPE0]], #[[$SCOPE2]]]
112+
%2 = load i32, ptr %arg1, !alias.scope !5, !noalias !8
113+
; CHECK: llvm.load
114+
; CHECK-SAME: alias_scopes = [#[[$SCOPE2]]]
115+
; CHECK-SAME: noalias_scopes = [#[[$SCOPE0]], #[[$SCOPE1]]]
116+
%3 = load i32, ptr %arg1, !alias.scope !6, !noalias !9
117+
ret void
118+
}
119+
120+
!0 = !{!"domain1"}
121+
!1 = !{!"scopeid1", !0, !"The first scope"}
122+
!2 = !{!"scopeid2", !0}
123+
!3 = !{!"scopeid3", !0}
124+
!4 = !{!1}
125+
!5 = !{!2}
126+
!6 = !{!3}
127+
!7 = !{!2, !3}
128+
!8 = !{!1, !3}
129+
!9 = !{!1, !2}
130+

0 commit comments

Comments
 (0)