1//===- LoopAnnotationTranslation.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 an MLIR loop annotations and
10// the corresponding LLVMIR metadata representation.
11//
12//===----------------------------------------------------------------------===//
13
14#ifndef MLIR_LIB_TARGET_LLVMIR_LOOPANNOTATIONTRANSLATION_H_
15#define MLIR_LIB_TARGET_LLVMIR_LOOPANNOTATIONTRANSLATION_H_
16
17#include "mlir/Dialect/LLVMIR/LLVMAttrs.h"
18#include "mlir/Dialect/LLVMIR/LLVMInterfaces.h"
19#include "mlir/Target/LLVMIR/ModuleTranslation.h"
20
21namespace mlir {
22namespace LLVM {
23namespace detail {
24
25/// A helper class that converts LoopAnnotationAttrs and AccessGroupAttrs into
26/// corresponding llvm::MDNodes.
27class LoopAnnotationTranslation {
28public:
29 LoopAnnotationTranslation(ModuleTranslation &moduleTranslation,
30 llvm::Module &llvmModule)
31 : moduleTranslation(moduleTranslation), llvmModule(llvmModule) {}
32
33 llvm::MDNode *translateLoopAnnotation(LoopAnnotationAttr attr, Operation *op);
34
35 /// Returns the LLVM metadata corresponding to an mlir LLVM dialect access
36 /// group attribute.
37 llvm::MDNode *getAccessGroup(AccessGroupAttr accessGroupAttr);
38
39 /// Returns the LLVM metadata corresponding to the access group attribute
40 /// referenced by the AccessGroupOpInterface or null if there are none.
41 llvm::MDNode *getAccessGroups(AccessGroupOpInterface op);
42
43 /// The ModuleTranslation owning this instance.
44 ModuleTranslation &moduleTranslation;
45
46private:
47 /// Returns the LLVM metadata corresponding to a llvm loop metadata attribute.
48 llvm::MDNode *lookupLoopMetadata(Attribute options) const {
49 return loopMetadataMapping.lookup(Val: options);
50 }
51
52 void mapLoopMetadata(Attribute options, llvm::MDNode *metadata) {
53 auto result = loopMetadataMapping.try_emplace(Key: options, Args&: metadata);
54 (void)result;
55 assert(result.second &&
56 "attempting to map loop options that was already mapped");
57 }
58
59 /// Mapping from an attribute describing loop metadata to its LLVM metadata.
60 /// The metadata is attached to Latch block branches with this attribute.
61 DenseMap<Attribute, llvm::MDNode *> loopMetadataMapping;
62
63 /// Mapping from an access group attribute to its LLVM metadata.
64 /// This map is populated on module entry and is used to annotate loops (as
65 /// identified via their branches) and contained memory accesses.
66 DenseMap<AccessGroupAttr, llvm::MDNode *> accessGroupMetadataMapping;
67
68 llvm::Module &llvmModule;
69};
70
71} // namespace detail
72} // namespace LLVM
73} // namespace mlir
74
75#endif // MLIR_LIB_TARGET_LLVMIR_LOOPANNOTATIONTRANSLATION_H_
76