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. 11 // 12 //===----------------------------------------------------------------------===// 13 14 #ifndef MLIR_LIB_TARGET_LLVMIR_LOOPANNOTATIONIMPORTER_H_ 15 #define MLIR_LIB_TARGET_LLVMIR_LOOPANNOTATIONIMPORTER_H_ 16 17 #include "mlir/Dialect/LLVMIR/LLVMDialect.h" 18 #include "mlir/Target/LLVMIR/ModuleImport.h" 19 20 namespace mlir { 21 namespace LLVM { 22 namespace detail { 23 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: LoopAnnotationImporter(ModuleImport & moduleImport,OpBuilder & builder)28 LoopAnnotationImporter(ModuleImport &moduleImport, OpBuilder &builder) 29 : moduleImport(moduleImport), builder(builder) {} 30 LoopAnnotationAttr translateLoopAnnotation(const llvm::MDNode *node, 31 Location loc); 32 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); 38 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; 44 45 /// The ModuleImport owning this instance. 46 ModuleImport &moduleImport; 47 48 private: 49 /// Returns the LLVM metadata corresponding to a llvm loop metadata attribute. lookupLoopMetadata(const llvm::MDNode * node)50 LoopAnnotationAttr lookupLoopMetadata(const llvm::MDNode *node) const { 51 return loopMetadataMapping.lookup(node); 52 } 53 mapLoopMetadata(const llvm::MDNode * metadata,LoopAnnotationAttr attr)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"); 59 } 60 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; 66 }; 67 68 } // namespace detail 69 } // namespace LLVM 70 } // namespace mlir 71 72 #endif // MLIR_LIB_TARGET_LLVMIR_LOOPANNOTATIONIMPORTER_H_ 73