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