1 //===- LoopAnnotationImporter.h ---------------------------------*- C++ -*-===//
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //===----------------------------------------------------------------------===//
9 // This file implements the translation between LLVMIR loop metadata and the
10 // corresponding MLIR representation.
12 //===----------------------------------------------------------------------===//
14 #ifndef MLIR_LIB_TARGET_LLVMIR_LOOPANNOTATIONIMPORTER_H_
15 #define MLIR_LIB_TARGET_LLVMIR_LOOPANNOTATIONIMPORTER_H_
17 #include "mlir/Dialect/LLVMIR/LLVMDialect.h"
18 #include "mlir/Target/LLVMIR/ModuleImport.h"
24 /// A helper class that converts llvm.loop metadata nodes into corresponding
25 /// LoopAnnotationAttrs and llvm.access.group nodes into AccessGroupAttrs.
26 class LoopAnnotationImporter
{
28 LoopAnnotationImporter(ModuleImport
&moduleImport
, OpBuilder
&builder
)
29 : moduleImport(moduleImport
), builder(builder
) {}
30 LoopAnnotationAttr
translateLoopAnnotation(const llvm::MDNode
*node
,
33 /// Converts all LLVM access groups starting from node to MLIR access group
34 /// attributes. It stores a mapping from every nested access group node to the
35 /// translated attribute. Returns success if all conversions succeed and
36 /// failure otherwise.
37 LogicalResult
translateAccessGroup(const llvm::MDNode
*node
, Location loc
);
39 /// Returns the access group attribute that map to the access group nodes
40 /// starting from the access group metadata node. Returns failure, if any of
41 /// the attributes cannot be found.
42 FailureOr
<SmallVector
<AccessGroupAttr
>>
43 lookupAccessGroupAttrs(const llvm::MDNode
*node
) const;
45 /// The ModuleImport owning this instance.
46 ModuleImport
&moduleImport
;
49 /// Returns the LLVM metadata corresponding to a llvm loop metadata attribute.
50 LoopAnnotationAttr
lookupLoopMetadata(const llvm::MDNode
*node
) const {
51 return loopMetadataMapping
.lookup(node
);
54 void mapLoopMetadata(const llvm::MDNode
*metadata
, LoopAnnotationAttr attr
) {
55 auto result
= loopMetadataMapping
.try_emplace(metadata
, attr
);
57 assert(result
.second
&&
58 "attempting to map loop options that was already mapped");
62 DenseMap
<const llvm::MDNode
*, LoopAnnotationAttr
> loopMetadataMapping
;
63 /// Mapping between original LLVM access group metadata nodes and the imported
64 /// MLIR access group attributes.
65 DenseMap
<const llvm::MDNode
*, AccessGroupAttr
> accessGroupMapping
;
72 #endif // MLIR_LIB_TARGET_LLVMIR_LOOPANNOTATIONIMPORTER_H_