[SampleProfileLoader] Fix integer overflow in generateMDProfMetadata (#90217)
[llvm-project.git] / mlir / lib / Target / LLVMIR / LoopAnnotationImporter.h
blob6c71c625b4ebb36450f9c02f000a518c47d01e5e
1 //===- LoopAnnotationImporter.h ---------------------------------*- C++ -*-===//
2 //
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
6 //
7 //===----------------------------------------------------------------------===//
8 //
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"
20 namespace mlir {
21 namespace LLVM {
22 namespace detail {
24 /// A helper class that converts llvm.loop metadata nodes into corresponding
25 /// LoopAnnotationAttrs and llvm.access.group nodes into AccessGroupAttrs.
26 class LoopAnnotationImporter {
27 public:
28 LoopAnnotationImporter(ModuleImport &moduleImport, OpBuilder &builder)
29 : moduleImport(moduleImport), builder(builder) {}
30 LoopAnnotationAttr translateLoopAnnotation(const llvm::MDNode *node,
31 Location loc);
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;
48 private:
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);
56 (void)result;
57 assert(result.second &&
58 "attempting to map loop options that was already mapped");
61 OpBuilder &builder;
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;
68 } // namespace detail
69 } // namespace LLVM
70 } // namespace mlir
72 #endif // MLIR_LIB_TARGET_LLVMIR_LOOPANNOTATIONIMPORTER_H_