73 lines · c
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-exception6//7//===----------------------------------------------------------------------===//8//9// This file implements the translation between LLVMIR loop metadata and the10// 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 20namespace mlir {21namespace LLVM {22namespace detail {23 24/// A helper class that converts llvm.loop metadata nodes into corresponding25/// LoopAnnotationAttrs and llvm.access.group nodes into AccessGroupAttrs.26class LoopAnnotationImporter {27public: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 group34 /// attributes. It stores a mapping from every nested access group node to the35 /// translated attribute. Returns success if all conversions succeed and36 /// failure otherwise.37 LogicalResult translateAccessGroup(const llvm::MDNode *node, Location loc);38 39 /// Returns the access group attribute that map to the access group nodes40 /// starting from the access group metadata node. Returns failure, if any of41 /// 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 48private: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);52 }53 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 imported64 /// MLIR access group attributes.65 DenseMap<const llvm::MDNode *, AccessGroupAttr> accessGroupMapping;66};67 68} // namespace detail69} // namespace LLVM70} // namespace mlir71 72#endif // MLIR_LIB_TARGET_LLVMIR_LOOPANNOTATIONIMPORTER_H_73