Skip to content

Commit 0c749fc

Browse files
committed
Fix nondeterminism, non-trivial nits
1 parent ac02871 commit 0c749fc

File tree

3 files changed

+23
-16
lines changed

3 files changed

+23
-16
lines changed

mlir/lib/Target/LLVMIR/Dialect/LLVMIR/LLVMIRToLLVMTranslation.cpp

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -220,19 +220,28 @@ static LogicalResult setDereferenceableAttr(const llvm::MDNode *node,
220220
/// discardable `llvm.mmra` attribute.
221221
static LogicalResult setMmraAttr(llvm::MDNode *node, Operation *op,
222222
LLVM::ModuleImport &moduleImport) {
223-
llvm::MMRAMetadata wrapper(node);
224-
if (wrapper.empty())
223+
if (!node)
225224
return success();
226-
225+
226+
// We don't use the LLVM wrappers here becasue we care about the order
227+
// of the metadata for deterministic roundtripping.
227228
MLIRContext *ctx = op->getContext();
229+
auto toAttribute = [&](llvm::MDNode *tag) -> Attribute {
230+
return LLVM::MMRATagAttr::get(
231+
ctx, cast<llvm::MDString>(tag->getOperand(0))->getString(),
232+
cast<llvm::MDString>(tag->getOperand(1))->getString());
233+
};
228234
Attribute mlirMmra;
229-
if (wrapper.size() == 1) {
230-
auto [prefix, suffix] = *wrapper.begin();
231-
mlirMmra = LLVM::MMRATagAttr::get(ctx, prefix, suffix);
235+
if (llvm::MMRAMetadata::isTagMD(node)) {
236+
mlirMmra = toAttribute(node);
232237
} else {
233238
SmallVector<Attribute> tags;
234-
for (auto [prefix, suffix] : wrapper)
235-
tags.push_back(LLVM::MMRATagAttr::get(ctx, prefix, suffix));
239+
for (const llvm::MDOperand &operand : node->operands()) {
240+
auto *tagNode = dyn_cast<llvm::MDNode>(operand.get());
241+
if (!tagNode || !llvm::MMRAMetadata::isTagMD(tagNode))
242+
return failure();
243+
tags.push_back(toAttribute(tagNode));
244+
}
236245
mlirMmra = ArrayAttr::get(ctx, tags);
237246
}
238247
op->setAttr(LLVMDialect::getMmraAttrName(), mlirMmra);

mlir/lib/Target/LLVMIR/Dialect/LLVMIR/LLVMToLLVMIRTranslation.cpp

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -736,13 +736,11 @@ amendOperationImpl(Operation &op, ArrayRef<llvm::Instruction *> instructions,
736736
tags.emplace_back(oneTag.getPrefix(), oneTag.getSuffix());
737737
} else if (auto manyTags = dyn_cast<ArrayAttr>(attribute.getValue())) {
738738
for (Attribute attr : manyTags) {
739-
auto tag = dyn_cast<MMRATagAttr>(a);
740-
if (tag) {
741-
tags.emplace_back(tag.getPrefix(), tag.getSuffix());
742-
} else {
739+
auto tag = dyn_cast<MMRATagAttr>(attr);
740+
if (!tag)
743741
return op.emitOpError(
744742
"MMRA annotations array contains value that isn't an MMRA tag");
745-
}
743+
tags.emplace_back(tag.getPrefix(), tag.getSuffix());
746744
}
747745
} else {
748746
return op.emitOpError(
@@ -754,7 +752,7 @@ amendOperationImpl(Operation &op, ArrayRef<llvm::Instruction *> instructions,
754752
// Empty list, canonicalizes to nothing
755753
return success();
756754
}
757-
for (llvm::Instruction *inst : instructions)
755+
for (llvm::Instruction *inst : instructions)
758756
inst->setMetadata(llvm::LLVMContext::MD_mmra, mmraMd);
759757
return success();
760758
}

mlir/test/Target/LLVMIR/Import/metadata-mmra.ll

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ define void @native(ptr %x, ptr %y) {
99
; CHECK-SAME: llvm.mmra = #[[$MMRA0]]
1010
%v = load i32, ptr %x, align 4, !mmra !0
1111
; CHECK: llvm.fence
12-
; CHECK-SAME: llvm.mmra = [#[[$MMRA0]], #[[$MMRA1]]]
12+
; CHECK-SAME: llvm.mmra = [#[[$MMRA1]], #[[$MMRA0]]]
1313
fence syncscope("workgroup-one-as") release, !mmra !2
1414
; CHECK: llvm.store {{.*}}, !llvm.ptr{{$}}
1515
store i32 %v, ptr %y, align 4, !mmra !3
@@ -18,5 +18,5 @@ define void @native(ptr %x, ptr %y) {
1818

1919
!0 = !{!"foo", !"bar"}
2020
!1 = !{!"amdgpu-synchronize-as", !"local"}
21-
!2 = !{!0, !1}
21+
!2 = !{!1, !0}
2222
!3 = !{}

0 commit comments

Comments
 (0)